//
//  JijinDetailController.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "JijinDetailController.h"

#define kJijinDetailUrl @"http://zhiboba.3b2o.com/program/HighlightsListJson/"

@interface JijinDetailController ()

@property(nonatomic,strong)NSMutableArray *modelArray;
@property(nonatomic,strong)UILabel *navTitle;

@end

@implementation JijinDetailController

- (void)loadData
{
    NSString *url = [NSString stringWithFormat:@"%@%@",kJijinDetailUrl,self.sid];
    
    [LORequestManger GET:url success:^(id response) {
        
        self.modelArray = [NSMutableArray array];
        
        NSDictionary *superDic = (NSDictionary *)response;
        NSArray *videos = superDic[@"videos"];
        for (NSDictionary *dic in videos) {
            JijinVideoDetailModel *model = [JijinVideoDetailModel modelWithDictionary:dic];
            [self.modelArray addObject:model];
        }
        
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self customNavigationBar];
    [self customHeaderView];
    
    [self loadData];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.showsVerticalScrollIndicator = NO;

    [self headerRefreshing];
}

- (void)headerRefreshing
{
    __unsafe_unretained typeof(self) vc = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            // 重新加载 刷新表格
            [vc loadData];
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [vc.tableView.header endRefreshing];
        });
    }];
}



- (void)customNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg_green_2@2x"] forBarMetrics:UIBarMetricsDefault];
    
    
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"set_arrow_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popback)];
    [backBarButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBarButton;
    
    self.navTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenIp6(50), kScreenIp6(30))];
    self.navTitle.text = @"集锦";
    self.navTitle.textColor = [UIColor whiteColor];
    [self.navigationItem setTitleView:_navTitle];
}

- (void)popback
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)customHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(155))];
    
    CustomHeaderView *headView = [[CustomHeaderView alloc] initWithFrame:CGRectMake(kScreenIp6(50), kScreenIp6(20), kScreenWidth - kScreenIp6(50)*2, kScreenIp6(60))];
    
    if (self.hostInfo.count != 0 && self.guestInfo.count != 0)
    {
        [headView.hostLogo setImageWithURL:[NSURL URLWithString:self.hostInfo[@"logo"]] placeholderImage:[UIImage imageNamed:@"zhudui_1@2x"]];
        headView.hostName.text = self.hostInfo[@"name"];
        [headView.guestLogo setImageWithURL:[NSURL URLWithString:self.guestInfo[@"logo"]] placeholderImage:[UIImage imageNamed:@"kedui_2@2x"]];
        headView.guestName.text = self.guestInfo[@"name"];
        
        
        if ([self.hostInfo[@"score"] length] && [self.guestInfo[@"score"] length])
        {
            headView.score.text = [NSString stringWithFormat:@"%@ : %@",self.hostInfo[@"score"],self.guestInfo[@"score"]];
        }
        else
        {
            headView.score.text = @"V S";
        }
        
        
        [headerView addSubview:headView];
        
        self.tableView.tableHeaderView = headerView;
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    [tableView registerClass:[JijinDetailCell class] forCellReuseIdentifier:cellIdentifier];
    JijinDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    [cell setModelWith:self.modelArray[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenIp6(110);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayViewController *playVc = [[PlayViewController alloc] init];
    
    playVc.playUrl = [self.modelArray[indexPath.row] defaultMobileLink];
    
    [self.navigationController pushViewController:playVc animated:YES];
}




// 隐藏 tabbar
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
}


@end
